import simpleGit, { SimpleGitOptions } from 'simple-git'
import createLogger from 'progress-estimator'
import chalk from 'chalk'
import figlet from 'figlet'
import log from './log'

//初始化进度条
const logger = createLogger({
  spinner: {
    interval: 100,
    frames: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'].map(item=>chalk.blue(item))
  }
})

const gitOptions: Partial<SimpleGitOptions> = {
  baseDir: process.cwd(),
  binary: 'git',
  maxConcurrentProcesses: 6,
};

function logoPrint() {
  figlet('rysx-cli', (error, data) => {
    if (error) {
      log.error('Something went wrong...')
      console.log(error)
      return
    }
    console.log(data)
  })
}

export async function clone(url: string, projectName: string,  options: string[]) {
  const git = simpleGit(gitOptions)

  try {
    await logger(git.clone(url, projectName, options), '代码下载中...', {
      estimate: 7000  // 预计下载时长
    })
    logoPrint()
    log.success(chalk.green('代码下载成功！！！'))
    console.log(chalk.green('======================='))
    log.info(chalk.green('==== 欢迎进入 rysx-custom-cli ===='))
    console.log(chalk.green('======================='))
    console.log()
    console.log()
    log.info(chalk.green('==== 请使用pnpm安装依赖 ===='))
    log.info(chalk.green('==== pnpm run dev运行项目 ===='))
  } catch (error) {
    console.log(chalk.red('代码下载失败')) 
  }
}